/*
 * Created on May 25, 2004
 * by Mihai Postelnicu
 * email mihai@code.ro
 *
 *
 */
package ro.gateway.aida.search;

import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import ro.gateway.aida.servlet.Constants;

/**
 * @author Mihai Postelnicu email mihai@code.ro
 */
public class CurrencyCell extends ResultCell {
	protected Long item;

	protected String symbol;

	protected String prettyAmount() {
		String s = item.toString();
		if (s.length() < 4)
			return s;
		String ret = "";

		for (int i = 0; i < s.length(); i++) {
			if ((s.length() - i) % 3 == 0 && i != 0)
				ret += ",";
			ret += s.charAt(i);
		}
		return ret;
	}

	public CurrencyCell(String type) {
		super(type);
	}

	public int compareTo(Object o) {
		CurrencyCell cc2 = (CurrencyCell) o;
		return item.compareTo(cc2.getItem());
	}

	public String toString() {
		if (item == null || symbol == null)
			return super.toString();
		return prettyAmount() + " " + symbol;
	}

	/**
	 * @return Returns the item.
	 */
	public Long getItem() {
		return item;
	}

	/**
	 * @param item
	 *            The item to set.
	 */
	public void setItem(long item) {
		this.item = new Long(item);
	}

	public void setItem(Long item) {
		this.item = item;
	}

	/**
	 * @return Returns the symbol.
	 */
	public String getSymbol() {
		return symbol;
	}

	/**
	 * @param symbol
	 *            The symbol to set.
	 */
	public void setSymbol(String symbol) {
		this.symbol = symbol;
	}

	public int appendXlsCells(WritableSheet sheet, int x, int y, boolean header)
			throws RowsExceededException, WriteException {
		if (header) {
			sheet.addCell(new Label(x, y, type.toUpperCase()+" (amount)",Constants.times12format));
			sheet.addCell(new Label(x+1, y, type.toUpperCase() + " (currency)",Constants.times12format));
		} else {
			sheet.addCell(new Number(x, y, item.longValue()));
			sheet.addCell(new Label(x+1, y, symbol));
		}
		return 2;
	}
}